import Vue from 'vue'
import Vuex from 'vuex'
import api from '../pages/main/api'

Vue.use(Vuex)
const debug = process.env.NODE_ENV !== 'production'

const rootState = {
  user: {}
}
const rootGetters = {
  authenticated (state) {
    return !!state.user.username
  }
}
const rootMutations = {
  setUser (state, payload) {
    state.user = payload.user
  },
  clearUser (state) {
    state.user = {}
  }
}
const rootActions = {
  getProfile ({commit}) {
    api.getProfile().then(resp => {
      commit('setUser', {user: resp.data.data})
    })
  }
}

export default new Vuex.Store({
  state: rootState,
  getters: rootGetters,
  mutations: rootMutations,
  actions: rootActions,
  strict: debug
})
